package com.ebelter.temperature.adapters;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.temperature.R;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by pan on 2018/7/17
 */

public class TBandDeviceAdapter extends BaseAdapter {

    private Context mContext;
    private List<BluetoothDevice> mDatas;

    public String bandAddress;
    public String bandName;




    public void addOne(BluetoothDevice bluetoothDevice){

        if (bluetoothDevice==null){
            return;
        }

        if (mDatas==null){
            mDatas=new ArrayList<>();
        }

        if (!isContain(bluetoothDevice)){
            mDatas.add(bluetoothDevice);
        }
        notifyDataSetChanged();

    }

    public boolean isContain(BluetoothDevice bluetoothDevice){
        if (mDatas==null||mDatas.size()==0){
            return false;
        }
        for (BluetoothDevice b:mDatas){
            if (TextUtils.equals(b.getAddress(),bluetoothDevice.getAddress())){
                return true;
            }
        }
        return false;
    }


    public List<BluetoothDevice> getmDatas() {
        return mDatas;
    }

    public TBandDeviceAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mDatas==null?0:mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        VH vh;
        if (convertView==null){
            vh=new VH();
            convertView=View.inflate(mContext, R.layout.item_band_devices,null);
            vh.name=convertView.findViewById(R.id.item_band_tv);
            vh.iv=convertView.findViewById(R.id.item_band_iv2);
            convertView.setTag(vh);

        }else {
            vh= (VH) convertView.getTag();
        }

        BluetoothDevice device=mDatas.get(position);
        vh.name.setText(device.getName()+"("+device.getAddress()+")");

        if (TextUtils.equals(bandAddress,device.getAddress())){
            ViewUtils.displayView(vh.iv);
        }else {
            ViewUtils.hideView(vh.iv);
        }

        return convertView;
    }


    class VH{

        TextView name;
        ImageView iv;
    }
}
